package com.swpu.eduservice.controller;

import com.swpu.commonutils.R;
import com.swpu.eduservice.entity.EduTeacher;
import com.swpu.eduservice.entity.vo.TeacherQuery;
import com.swpu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ygq
 * @since 2021-10-11
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    //注入Service
    @Autowired
    private EduTeacherService eduTeacherService;

    //查询所有的讲师数据
    //rest风格
    @ApiOperation(value = "查询所有的讲师")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R deleteTeacher(@PathVariable String id){

        boolean b = eduTeacherService.removeById(id);
        if(b) return R.ok();
        else return R.error();
    }

    @ApiOperation(value = "讲师分页功能")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        //定义分页对象
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
        //调用方法实现分页 把查询到的数据分页的数据放在teacherPage中
        eduTeacherService.page(teacherPage,null);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);

    }

    //条件查询带分页的方法
    @ApiOperation("分页条件查询讲师")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R PageTeacherCondition(@PathVariable("current")long current,
                                  @PathVariable("limit")long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> teacherPage  = new Page<>();
        //构造条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified",end);
        }
        //调用分页查询
        eduTeacherService.page(teacherPage,queryWrapper);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }

    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save) return R.ok().success(true);
        else return R.error();
    }

    //根据id查询讲师数据
    @ApiOperation("查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getByID(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        if(teacher!=null) return R.ok().data("teacher",teacher);
        else return R.error();
    }

    //讲师的修改
    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public R UpdateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update) return R.ok();
        else return R.error();

    }

}

